<?php
class RM_UnitDiscounts extends RM_Model {
    protected $_name = 'rm_unit_discounts';

    function insertRows(RM_Discounts_Row $discount, array $unitIDs){
        $this->delete('discount_id='.$discount->id); //remove old values
        if ($unitIDs[0] == 0) {
            //all units
            $discount->global = 1;
            $discount->save();
            return true;
        } elseif ($discount->global == 1) {
            $discount->global = 0;
            $discount->save();
        }

        foreach ($unitIDs as $unitID) {
            $this->createRow(array(
                'discount_id' => $discount->id,
                'unit_id' => $unitID,
            ))->save();
        }
    }

    function getByDiscount(RM_Discounts_Row $discount){
        return $this->fetchAll($this->select()->where('discount_id=?', $discount->id));        
    }        
}